package com.zhucx.loader.strategy;

import com.microsoft.playwright.*;
import com.zhucx.loader.PageLoader;
import com.zhucx.model.PageRequest;
import com.zhucx.utils.RegexUtil;
import com.zhucx.utils.UrlUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class PlaywrightPageLoader extends PageLoader {
    private static Logger logger = LoggerFactory.getLogger(SeleniumPhantomjsPageLoader.class);

    @Override
    public Document load(PageRequest pageRequest) {
        if (!UrlUtil.isUrl(pageRequest.getUrl())) {
            return null;
        }
        Document doc = null;
        Map<String,String> map = new HashMap<String,String>();
//        //跳过下载浏览器，因为公司是内网，这个配置很重要
//        map.put("PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD", "1");
//        //跳过下载浏览器后配置浏览器位置
//        map.put("PLAYWRIGHT_BROWSERS_PATH", "D:\\pw-browsers\\ms-playwright");
        try (Playwright playwright = Playwright.create(new Playwright.CreateOptions().setEnv(map))) {

            String pageUrl = pageRequest.getUrl();
//            Browser browser = playwright.chromium().launch(
//                    new BrowserType.LaunchOptions().setHeadless(true) //无头模式
//                            .setSlowMo(100) //减慢执行速度，以免太快
//                            .setDevtools(true)); //打开浏览器开发者工具，默认不打开
            Browser browser = playwright.chromium().launch();
            BrowserContext context = browser.newContext();
            Page page = context.newPage();
            page.navigate(pageUrl);
            String pageContent = page.content();
            doc = Jsoup.parse(pageContent,pageUrl);
            browser.close();
            playwright.close();
        }

        return doc;
    }






    public static String getContentByPlaywright(String trueUrl){
        Boolean isTrue = RegexUtil.isUrl(trueUrl);
        if(!isTrue){
            System.out.println("url存在问题"+trueUrl);
            return "";
        }
        Page page = initPage();
        try {
            page.navigate(trueUrl);
            String content = page.content();
            return content;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            BrowserContext context = page.context();
            Browser browser = context.browser();
            page.close();
            context.close();
            browser.close();
        }
        return "";
    }

    public static Page initPage(){
        return initPage("chromium");
    }

    public static Page initPage(String browserName){
        Playwright playwright = Playwright.create();
        Browser browser = createBrowser(browserName, playwright);
        BrowserContext browserContext = createContext(browser);
        Page page = browserContext.newPage();
        return page;
    }


    public static BrowserContext createContext(Browser browser) {
        BrowserContext context = browser.newContext(new Browser.NewContextOptions()
                .setIgnoreHTTPSErrors(true)
                .setJavaScriptEnabled(true)
                .setViewportSize(1920, 1080)
                .setUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36"));
        return context;
    }

    public static Browser createBrowser(String name, Playwright playwright) {
        try {
            switch (name) {
                case "firefox":
                    return playwright.firefox().launch();
                case "chromium":
                    return playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false).setTimeout(120 * 1000));
                case "webkit":
                    return playwright.webkit().launch();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
